package com.beasts.ant.auth.config.oauth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.ExpressionUrlAuthorizationConfigurer;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * oauth web配置
 *
 * @author Locker
 * @date 07/05/2018 10:22 AM
 * @since 1.0
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthUrlsConfig authUrlsConfig;

    @Autowired
    private UserDetailsService userDetailsService;

    /**
     * 匹配用户时的加密规则
     */
    public PasswordEncoder passwordEncoder() {
        // 标准密码加密 SHA-256
//        return new StandardPasswordEncoder();
        // MD5 加密
//        return new Md5PasswordEncoder();
        // 无密码加密
        return NoOpPasswordEncoder.getInstance();
    }

    /**
     * 配置全局设置
     */
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        // 设置UserDetailsService及密码规则
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * http请求拦截
     *
     * @param http
     * @throws Exception
     */
    @Override
    public void configure(HttpSecurity http) throws Exception {
        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry =
                http.formLogin()
                        // 指定登录路径
//                        .loginPage("/self/login")
                        // 指定登录处理路径
//                        .loginProcessingUrl("/index")
                        .and()
                        .authorizeRequests();

        // 排除指定路径拦截，见配置文件
        for (String url : authUrlsConfig.getAnon()) {
            registry.antMatchers(url).permitAll();
        }

        // 拦截除上边配置放过路径外的全部请求
        registry.anyRequest().authenticated();

        // 创建session
//        registry.and()
//                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED);

        // 关闭跨站请求伪造，spring默认开启
        registry.and()
                .csrf().disable();

        // 原始写法
//        http.requestMatchers().anyRequest()
//                .and()
//                .authorizeRequests()
//                .antMatchers("/oauth/*").permitAll()
//                .antMatchers("/secure/**").authenticated();

    }

    /**
     * 忽略请求
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                // 开启全部js访问
                .antMatchers("/js/**/*.{js,html}")
                .antMatchers("/i18n/**")
                .antMatchers("/auth/**")
        ;
    }

}
